//
//  WQStatusViewModel.swift
//  WeiBo
//
//  Created by 张德华 on 7/29/16.
//  Copyright © 2016 ZhangDeHua. All rights reserved.
//

import UIKit

class WQStatusViewModel: NSObject {
    
    /**status属性*/
    var status: WQStatus?
    
    /**微博认证用户图片*/
    var avatarImage: UIImage?
    /**处理会员等级图片*/
    var memberImage: UIImage?
    /**微博来源处理富文本*/
    var sourceAttr: NSMutableAttributedString?
    /**转发数*/
    var repostsCountStr: String?
    /**评论数*/
    var commentsCountStr: String?
    /**表态数*/
    var attitudesCountStr: String?
    /**微博事件(计算型)*/
    var sinaTimeStr: String? {
        
        return NSDate.sinDate(status?.created_at ?? "")?.sinaDescripitionStr
    }
    
    // MARK: - ************重载构造函数 init
    init(status: WQStatus) {
        super.init()
        
        self.status = status
        
        self.avatarImage = getAvatarImage(status.user?.verified)
        self.memberImage = getMemberImage(status.user?.mbrank)
        self.sourceAttr = getSourceStr(status.source!)
        self.repostsCountStr = getSinaCount(status.reposts_count, title: "转发")
        self.commentsCountStr = getSinaCount(status.comments_count, title: "评论")
        self.attitudesCountStr = getSinaCount(status.attitudes_count, title: "赞")
    }
    
}


extension WQStatusViewModel {
    
    // MARK: - ************处理微博转发评论赞的业务逻辑
    func getSinaCount(count: Int, title: String) -> String {
        
        ///------ 如果count <= 0
        if count <= 0 {
            return title
        } else {
            
            if count < 10000 {
                
                return "\(count)"
            } else {
                
                let sinaCount = CGFloat(count/1000)/10
                
                ///------ 字符串拼接
                var sinaCountStr = "\(sinaCount)万"
                ///------ 如果字符串包含.0
                if sinaCountStr.containsString(".0") {
                    
                    ///------ 字符串替换
                    sinaCountStr = sinaCountStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                }
                return sinaCountStr
            }
        }
    }
}


// MARK: - ************处理数据
extension WQStatusViewModel {
    
    // MARK: - ************处理微博认证用户图片
    func getAvatarImage(verified: Int?) -> UIImage? {
        
        ///------ 判断是否为nil
        guard let ver = verified else {
            return nil
        }
        
        switch ver {
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default:
            return UIImage(named: "avatar_vgirl")
        }
    }
    
    
    // MARK: - ************处理会员等级图片
    func getMemberImage(mbrank: Int?) -> UIImage? {
        
        ///------ 判断是否为nil
        guard let mbr = mbrank else {
            return nil
        }
 
        if mbr > 0 && mbr < 7 {
            return UIImage(named: "common_icon_membership_level\(mbr)")
        }
        
        return UIImage(named: "common_icon_membership")
    }
}


// MARK: - ************当前认为是MVVM开发
extension WQStatusViewModel {
    
    func getSourceStr(source: String?) -> NSMutableAttributedString {
        
        ///------ 判断是否为nil,而且是否保存
        guard let s = source where s.containsString("\">") else {
            
            let str = "上海三期微博"
            return getSourceAttr(str)
        }
        
        ///------ 起始位置和结束位置
        let startRange = s.rangeOfString("\">")!
        let endRange = s.rangeOfString("</")!
        
        ///------ 字符串截取
        let result = s.substringWithRange(startRange.endIndex..<endRange.startIndex)
        
        return getSourceAttr(result)
    }
    
    func getSourceAttr(result: String) -> NSMutableAttributedString{
        
        ///------ 最终显示格式
        let name = "来自" + result
        ///------ 得到range
        let range = (name as NSString).rangeOfString("来自")
        ///------ 创建富文本
        let attr = NSMutableAttributedString(string: name)
        ///------ 设置属性
        attr.addAttributes([NSForegroundColorAttributeName: UIColor.grayColor()], range: range)
        
        return attr
    }}







